package data_structure.leetcode;

public class Solution1011 {

    public int shipWithinDays(int[] weights, int D) {

        int max=0;
        for(int weight:weights){
            max= Math.max (weight, max);
        }

        int l=max;
        int r=Integer.MAX_VALUE;
        while(l<r){
            int mid=l+(r-l)/2;
            if(days (weights,mid)>D){
                l=mid+1;
            }else{
                r=mid;
            }
        }
        return l;

    }

    private int days(int []goods,int weight){

        //weight---对应的是一天
        int result=0;
        int indexTemp=0;
        for(int i=0;i<goods.length;){
            int temp=weight;
            while(true){
                if(i>=goods.length||temp-goods[i] < 0){
                    result++;
                    break;
                }
                temp-=goods[i];
                i++;
                indexTemp=i;
            }
            i=indexTemp;
        }
        return result;
    }
    public static void main(String[] args) {
        int []arr=new int[]{1,2,3,4,5,6,9,10};
        Solution1011 solution1011 = new Solution1011();
        int days=solution1011.days(arr,11);
        int i=0;
    }

}
